package com.ihelp.controller;

import com.ihelp.annotation.UserCheckLogin;
import com.ihelp.annotation.UserCheckOperate;
import com.ihelp.entities.CommonResult;
import com.ihelp.pojo.RequestDTO;
import com.ihelp.service.RequestService;
import com.ihelp.util.JwtUtil;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LiuZiMing
 * @since 2023-05-23
 */
@RestController
@Slf4j
@RequestMapping(value = "/request")
public class RequestController {
    @Autowired
    private RequestService requestService;
    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping(value = "/all/page/{pageId}", produces = "application/json;charset=utf-8")
    public CommonResult getAllRequest(@PathVariable int pageId) {
        List<RequestDTO> allRequest = requestService.getAllRequest(pageId);
        if (allRequest != null) {
            return new CommonResult<>(200, "根据分页:" + pageId + ",获取所有医陪请求成功", allRequest);
        } else {
            return new CommonResult(204, "当前分页:" + pageId + ",不存在医陪请求");
        }

    }

    @GetMapping("/all/page/{pageId}/Conditions")
    public CommonResult getAllRequestByDynamicConditions(@RequestParam(value = "hospitalId", required = false) Integer hospitalId,
                                                         @RequestParam(value = "hospitalDepartmentId", required = false) Integer hospitalDepartmentId,
                                                         @RequestParam(value = "visitTime", required = false) String visitTime,
                                                         @RequestParam(value = "visitorId", required = false) Integer visitorId,
                                                         @RequestParam(value = "price", required = false) Double price,
                                                         @RequestParam(value = "serviceTypeId", required = false) Integer serviceTypeId,
                                                         @RequestParam(value = "areaId") int areaId,
                                                         @RequestParam(value = "duration") int duration,
                                                         @PathVariable(value = "pageId") int pageId

    ) {
        List<RequestDTO> allRequest = requestService.getAllRequestByDynamicConditions(pageId, hospitalId, hospitalDepartmentId, visitTime, visitorId, price, serviceTypeId, areaId,duration);
        if (allRequest.isEmpty()) {
            return new CommonResult<>(200, "根据分页:" + pageId + ",条件:hospitalId：" + hospitalId + " hospitalDepartmentId：" + hospitalDepartmentId
                    + " visitTime：" + visitTime + " visitorId：" + visitorId + " price：" + price + " serviceTypeId：" + serviceTypeId + " areaId：" + areaId + " 获取所有医陪请求成功",
                    allRequest);
        } else {
            return new CommonResult<>(204, "根据信息找不到医陪请求");
        }

    }

    @UserCheckOperate(operateObject = "request")
    @GetMapping("/id/{id}")
    public CommonResult getRequestById(@RequestParam String userJWT, @PathVariable Integer id) {
        try {
            RequestDTO requestById = requestService.getRequestById(id);
            Integer visitorId = requestById.getVisitorId();
            return new CommonResult<>(200, "根据id：" + id + "获取请求成功", visitorId);
        } catch (Exception e) {
            return new CommonResult<>(204, "不存在id："+id+"的请求");
        }
    }

    @DeleteMapping("/id/{id}")
    public CommonResult deleteRequestById(@PathVariable Integer id) {
            Integer result = requestService.deleteRequestById(id);
            if (result != null && result == 1) {
                return new CommonResult<>(200, "根据id：" + id + "删除请求成功");
            } else {
                return new CommonResult<>(400, "根据id：" + id + "删除请求失败");
            }
    }

    @PostMapping("/")
    public CommonResult createRequest(@RequestParam("hospitalId") Integer hospitalId,
                                      @RequestParam("hospitalDepartmentId") Integer hospitalDepartmentId,
                                      @RequestParam("visitTime") String visitTime,
                                      @RequestParam("price") Double price,
                                      @RequestParam("describes") String describes,
                                      @RequestParam("serviceTypeId") Integer serviceTypeId,
                                      @RequestParam(value = "areaId") int areaId,
                                      @RequestParam(value = "duration") Double duration,
                                      HttpSession session) {

        if (session.getAttribute("userId") != null) {
            Integer visitorId = Integer.valueOf(session.getAttribute("userId").toString());
            Integer result = requestService.createRequest(hospitalId, hospitalDepartmentId, visitTime, price, describes, serviceTypeId, visitorId, areaId,duration);
            if (result != null && result == 1) {
                return new CommonResult<>(200, "根据:hospitalId：" + hospitalId + " hospitalDepartmentId：" + hospitalDepartmentId
                        + " visitTime：" + visitTime + " visitorId：" + visitorId + " price：" + price + " describe：" + describes +
                        " serviceTypeId：" + serviceTypeId + " 创建新请求成功",
                        result);
            } else {
                return new CommonResult<>(400, "根据:hospitalId：" + hospitalId + " hospitalDepartmentId：" + hospitalDepartmentId
                        + " visitTime：" + visitTime + " visitorId：" + visitorId + " price：" + price + " describe：" + describes +
                        " serviceTypeId：" + serviceTypeId + " 创建新请求失败");
            }
        } else {
            return new CommonResult<>(400, "用户未登录");
        }


    }
}
